package com.video.controller;


import com.video.support.base.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import zhibi.common.support.base.Upload;
import zhibi.common.util.JsonUtil;
import zhibi.common.util.io.FileUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by 执笔 on 2017/1/11.
 * 工具控制器
 */
@Controller
public class ToolController extends BaseController {

    /**
     * 上传圖片  kindeditor
     *
     * @return
     * @Date 2016年8月25日下午3:47:15
     */
    @RequestMapping("uploadFile")
    public void upload(HttpServletResponse response, @RequestParam MultipartFile[] imgFile) {
        Upload upload = new Upload();
        upload.setError(1);
        for (MultipartFile item : imgFile) {
            if (imgFile == null || item.isEmpty()) {
                upload.setMessage("请选择文件");
                try {
                    response.getOutputStream().write(JsonUtil.objToJson(upload).getBytes());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    String path = FileUtil.save(filePath(), item);
                    upload.setUrl("/data/" + path);
                    upload.setError(0);
                    response.getOutputStream().write(JsonUtil.objToJson(upload).getBytes());
                } catch (Exception e) {
                    e.printStackTrace();
                    upload.setMessage(e.getMessage());
                    try {
                        response.getOutputStream().write(JsonUtil.objToJson(upload).getBytes());
                    } catch (IOException e1) {
                    }
                }
            }
        }
    }


}
